//
//  NavigationSystemViewController.m
//  SDKDemos
//
//  Created by Quan Lam on 11/15/13.
//
//

#import "NavigationSystemViewController.h"
#import "GoogleAPICaller.h"
#import "GoogleAPIToolBox.h"
#import <GoogleMaps/GoogleMaps.h>
#import "GoogleAPIMapRoute.h"
#import "Place.h"
#import "PlaceAnnotation.h"

#import "APIService.h"

@interface NavigationSystemViewController ()

@end

@implementation NavigationSystemViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
}

- (void)trafficStuffAPIRequestFinished:(NSMutableArray*)data
{
    for (int i = 0; i < data.count; i++) {
        NSDictionary* item = [data objectAtIndex:i];
        
        CLLocation *location = [[CLLocation alloc] initWithLatitude: [[item objectForKey:@"latitude"] floatValue] longitude:[[item objectForKey:@"longitude"] floatValue]];
        Place *currentPlace = [[Place alloc] initWithLocation:location reference:@"key" name:@"My Location" address:@"add"];
        PlaceAnnotation *annotation = [[PlaceAnnotation alloc] initWithPlace:currentPlace];
        [_mapView addAnnotation:annotation];
        
    }
}
-(void)serviceRequestTimeOut
{
    NSLog(@"Time out");
}
- (void) navigateButtonPressed:(id)sender {
    [GoogleAPIToolBox setCallerDelegate:self];
    [[GoogleAPIToolBox googleAPIToolBox] directionsAPICallfrom:[[self fromField] text] to:[[self toField] text]];
   /* GoogleAPICaller* myCaller = [[GoogleAPICaller alloc] init];
    myCaller.delegate = self;
    [myCaller directionsAPICallfrom:self.fromField.text to:self.toField.text];*/
    
}
-(void) myLocationButtonPressed:(id)sender{
    [GoogleAPIToolBox setMyLocationDelegate:self];
    [[GoogleAPIToolBox googleAPIToolBox] getCurrentLocation];
    [[GoogleAPIToolBox googleAPIToolBox] requestAllTrafficDataForDelegate:self];
    
}
-(void)weatherConditionRequestFinish:(NSMutableArray*) busStationArr
{
    [busStationArr retain];
    
}
-(void)directionsAPIRequestFinished: (NSDictionary*) data {
    //[[self textView] setText:[NSString stringWithFormat:@"%@",data]];
    GoogleAPIMapRoute *newRoute = [[GoogleAPIMapRoute alloc] initWithAPIResult:data];
    [[self textView] setText:[NSString stringWithFormat:@"%@",newRoute.routeContent]];
}

-(void)updateLocationRequestFinished:(NSArray *)locations {
    CLLocation *loc = [locations lastObject];
    
    MKCoordinateSpan span = MKCoordinateSpanMake(0.14, 0.14);
    MKCoordinateRegion region = MKCoordinateRegionMake([loc coordinate], span);
    

    [_mapView setRegion:region animated:YES];
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];
    Place *currentPlace = [[Place alloc] initWithLocation:location reference:@"key" name:@"My Location" address:@"add"];
    PlaceAnnotation *annotation = [[PlaceAnnotation alloc] initWithPlace:currentPlace];
    [_mapView addAnnotation:annotation];
    
    [[GoogleAPIToolBox googleAPIToolBox] reverseGeocode:[loc coordinate]  withDelegate:self];

}

-(void)reverseGeocodeRequestFinished:(GMSReverseGeocodeResponse *)response {
    [[self textView] setText:[NSString stringWithFormat:@"%@",response.firstResult.addressLine1]];
    
}


@end
